package com.yy.util.common;

import android.util.Log;

import com.yy.util.log.AppLog;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * Created by Administrator on 2015/6/3.
 */
public class DateParser {

    public static final String DATE_FORMAT_0 = "yyyy-MM-dd";
    public static final String DATE_FORMAT_1 = "yyyy/MM/dd";
    public static final String DATE_FORMAT_2 = "yyyy年MM月dd日";
    public static final String DATE_FORMAT_3 = "yyyyMMdd";

    public static final String TIME_FORMAT_0 = "HH:mm:ss";
    public static final String TIME_FORMAT_1 = "HHmmss";

    private String mDateFormat;
    private SimpleDateFormat mSimpleDateFormat;

    public DateParser(String dateFormat) {
        this.mDateFormat = dateFormat;
        mSimpleDateFormat = new SimpleDateFormat(dateFormat);
    }

    public void setDateFormat(String mDateFormat) {
        this.mDateFormat = mDateFormat;
    }

    public String format(String dateString) {
        Date date = parseString2Date(dateString);
        return mSimpleDateFormat.format(date);
    }

    public boolean contrastDate(String firstValue, String secondValue) {
        long times = calculateMarginTime(firstValue, secondValue);
        return times > 0;
    }

    public String getTimestamp() {
        setDateFormat(DATE_FORMAT_3 + TIME_FORMAT_1);
        Date date = new Date();
        String dateString = mSimpleDateFormat.format(date);
        String subDateString = dateString.substring(2, dateString.length());
        Random random = new Random();
        int value = random.nextInt(10000);
        return subDateString + value;
    }

    public boolean isSameYear(String firstValue, String secondValue) {
        final int marginYear = calculateMarginDays(firstValue, secondValue);
        AppLog.d("DateParser", marginYear + "");
        return marginYear >= 0 ? marginYear <= 365 : marginYear >= -365;
    }

    public boolean isSameMoth(String firstValue, String secondValue) {
        final int marginDays = calculateMarginDays(firstValue, secondValue);
        AppLog.d("DateParser", marginDays + "");
        return marginDays >= 0 ? marginDays <= 31 : marginDays >= -31;
    }

    /**
     * 检查两个时间是否在同一天
     * @param firstValue
     * @param secondValue
     * @return
     */
    public boolean isSameDays(String firstValue, String secondValue) {
        final long[] dates = parser(firstValue, secondValue);
        return dates[0] == dates[1];
    }

    /**
     * 获得当前时间
     * @return
     */
    public String getNowDate() {
        final Date date = new Date();
        return mSimpleDateFormat.format(date);
    }

    public int calculateMarginDays(String startDate, String endDate) {
        long marginTime = calculateMarginTime(startDate, endDate);
        return (int) (marginTime / 1000 / 3600 / 24);
    }

    public long calculateMarginTime(String startDate, String endDate) {
        long[] dates = parser(startDate, endDate);
        return dates[0] - dates[1];
    }

    private long[] parser(String... dateValue) {
        long[] results = new long[dateValue.length];
        for(int i = 0; i < dateValue.length; i++) {
            Date dates = parseString2Date(dateValue[i]);
            results[i] = dates.getTime();
        }
        return results;
    }

    private Date parseString2Date(String date) {
        final ParsePosition parsePosition = new ParsePosition(0);
        return mSimpleDateFormat.parse(date, parsePosition);
    }
}
